 set more off
use "C:\Users\Andy Baker\OneDrive - UCB-O365\Stata\Barry\Final\OmnibusBoth.dta", clear
keep wave id v16g_conversefamily v16e_conversefriends  v16b_conversenotamob v16i_converseamob 
keep if wave==3 | wave==6

recode v16g_conversefamily v16e_conversefriends  v16b_conversenotamob v16i_converseamob (4=0) (3=1) (2=2) (1=3) (else=.)
egen E_NeighTalk_F=rowmax(v16b_conversenotamob v16i_converseamob)
gen A_A_CountryYear=100 if wave==3
replace A_A_CountryYear=101 if wave==6
rename v16g_conversefamily E_FamTalk_F
rename v16e_conversefriends  E_FriendTalk_F
gen L_WtWithin=1
drop wave v16b_conversenotamob v16i_converseamob

save "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Data\CNEP\BraTwoCityTEMP.Fin.dta", replace

use "C:\Users\Andy baker\OneDrive - UCB-O365\Data\Brazil\BEPS 2014\BEPS 2014 Merged Data v3.dta", clear
gen A_A_CountryYear=102
svyset [pweight=weight_combined]
recode disc1 disc2 disc3 (4=0) (3=1) (2=2) (1=3) (else=.)
svy: mean disc1 disc2 disc3
svy: prop disc1 disc2 disc3

rename disc1 E_FamTalk_F
rename disc2 E_FriendTalk_F

gen L_WtWithin=weight_combined
	*just checking if urban talks more than rural
recode urb_rur 2=0
reg E_FamTalk_F urb_rur
reg E_FriendTalk_F urb_rur
keep if wave==1
save "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Data\CNEP\Bra2014TEMP.Fin.dta", replace


use "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Data\CNEP\merge37.dta"
rename  a_a_wtcombined A_A_WtCombined
rename a_a_countryyear A_A_CountryYear
rename e_famtalk E_FamTalk_F
rename e_friendtalk E_FriendTalk_F
rename e_neightalk E_NeighTalk_F
rename e_worktalk E_WorkTalk_F
rename l_income L_Income_F
rename l_housing L_Housing_F 
rename l_education L_Education_F
rename l_gender L_Gender_F
rename d_campaper D_CamPaper_F
rename d_camradio D_CamRadio_F
rename d_camtv D_CamTV_F
rename i_gov1contactpers I_Gov1Pers_F  
rename i_gov2contactpers I_Gov2Pers_F  
rename i_gov3contactpers I_Gov3Pers_F  
rename i_gov4contactpers I_Gov4Pers_F
rename i_gov5contactpers I_Gov5Pers_F
rename i_gov6contactpers I_Gov6Pers_F
rename i_opp1contactpers I_Opp1Pers_F 
rename i_opp2contactpers I_Opp2Pers_F 
rename i_opp3contactpers I_Opp3Pers_F  
rename i_opp4contactpers I_Opp4Pers_F  
rename i_opp5contactpers I_Opp5Pers_F
rename i_opp6contactpers I_Opp6Pers_F
rename i_opp7contactpers I_Opp7Pers_F
*rename H_InfoTest1_Z H_Infotest1_F
*rename H_InfoTest2_Z H_Infotest2_F
*rename H_InfoTest3_Z H_Infotest3_F
rename h_votewhichrecent H_VoteWhichRecent_F
rename h_informed H_Informed_F
rename h_infotest H_InfoTest_F
rename e_disc1know_f E_Disc1Know_F 
rename e_disc2know_f E_Disc2Know_F 
rename e_disc1freq E_Disc1Freq_F 
rename e_disc2freq E_Disc2Freq_F 
rename e_spouknow_f E_SpouKnow_F 
rename e_spoutalk E_SpouTalk_F 
rename h_interestcam_f H_InterestCam_F 
rename l_wtwithin L_WtWithin
rename l_neighborhood_f L_Neighborhood_F
rename e_spouvote2 E_SpouVote2_F
rename e_disc1part E_Disc1Part_F
rename e_disc2part E_Disc2Part_F
rename d_pap1part D_Pap1Part_F
rename d_rad1part D_Rad1Part_F  
rename d_tv1part D_TV1Part_F
rename e_spouvote E_SpouVote_F
rename a_a_idcountry A_A_IDCountry 

append using "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Data\CNEP\BraTwoCityTEMP.Fin.dta", gen(brazil)
append using "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Data\CNEP\Bra2014TEMP.Fin.dta", gen(brazil2014)


sort A_A_CountryYear A_A_IDCountry 
bysort A_A_CountryYear : gen id1=_n
replace A_A_IDCountry =id1 if A_A_IDCountry ==.
merge 1:1 A_A_CountryYear A_A_IDCountry using "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Data\CNEP\MergeCHL2000.dta"
drop _merge

replace L_Income_F=L_Income_Fch00 if  A_A_CountryYear  ==4
replace L_Housing_F=L_Housing_Fch00 if  A_A_CountryYear  ==4 
replace L_Neighborhood_F=L_Neighborhood_Fch00 if  A_A_CountryYear ==4 

merge 1:1 A_A_CountryYear A_A_IDCountry using "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Data\CNEP\Merge3LatAm.dta", update
drop _merge

merge 1:1 A_A_CountryYear A_A_IDCountry using "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Data\CNEP\MergeMex2018.dta", update
drop _merge

merge 1:1 A_A_CountryYear A_A_IDCountry using "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Data\CNEP\CN4DomRep2010merge.Fin.dta", update
drop _merge

merge 1:1 A_A_CountryYear A_A_IDCountry using "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Data\CNEP\CN4Mexico2012merge.Fin.dta", update

*-------------------------------------------------------------------------------	
*DISCUSSANT KNOWLEDGE
mvdecode E_Disc1Know_F E_Disc2Know_F E_Disc1Freq_F E_Disc2Freq_F E_SpouKnow_F E_SpouTalk_F H_InterestCam_F H_Informed_F, mv(995)
mvdecode E_Disc1Know_F E_Disc2Know_F E_Disc1Freq_F E_Disc2Freq_F E_SpouKnow_F E_SpouTalk_F H_InterestCam_F H_Informed_F, mv(999)
recode A_A_CountryYear 1=1 3=1 4=1 19=1 30=1 31=1 32=1 33=1 37=1 56=1 else=0, gen(latam)


irt 2pl H_Infotest1_F H_Infotest2_F H_Infotest3_F H_Infotest4_F if A_A_CountryYear==1   
predict aware1 if A_A_CountryYear==1, latent ebmeans 

irt 2pl H_Infotest1_F H_Infotest2_F H_Infotest3_F H_Infotest4_F if A_A_CountryYear==3   
predict aware3 if A_A_CountryYear==3, latent ebmeans 

irt 2pl H_Infotest1_F H_Infotest2_F H_Infotest3_F H_Infotest4_F if A_A_CountryYear==19  
predict aware19 if A_A_CountryYear==19, latent ebmeans  

pca H_Infotest1_F H_Infotest2_F H_Infotest3_F if A_A_CountryYear==32, com(1)
predict aware32 if A_A_CountryYear==32

irt 2pl H_Infotest1_F H_Infotest2_F H_Infotest3_F H_Infotest4_F H_Infotest5_F if A_A_CountryYear==33
predict aware33 if A_A_CountryYear==33, latent ebmeans  

irt 2pl H_Infotest1_F H_Infotest2_F H_Infotest3_F H_Infotest4_F H_Infotest5_F if A_A_CountryYear==56
predict aware56 if A_A_CountryYear==56, latent ebmeans  

egen aware1z=std(aware1) if A_A_CountryYear==1
egen aware3z=std(aware3) if A_A_CountryYear==3
egen aware19z=std(aware19) if A_A_CountryYear==19
egen aware32z=std(aware32) if A_A_CountryYear==32
egen aware33z=std(aware33) if A_A_CountryYear==33
egen aware56z=std(aware56) if A_A_CountryYear==56

egen aware4z=std(H_InfoTest_F) if A_A_CountryYear==4
egen aware30z=std(H_InfoTest_F) if A_A_CountryYear==30
egen aware31z=std(H_InfoTest_F) if A_A_CountryYear==31
egen aware37z=std(H_InfoTest_F) if A_A_CountryYear==37

egen aware=rowmax(aware1z aware3z aware19z aware32z aware33z aware56z aware4z aware30z aware31z aware37z)

*impute awaretemp H_Informed_F CountryYear1 CountryYear3 CountryYear19 CountryYear30 CountryYear32 CountryYear33 CountryYear37 if latam==1, gen(aware)

/*
recode H_Infotest_F 5/10=4
gen H_Informed=H_Informed_F if latam==1
polychoricpca H_Informed H_Infotest_F, score(awaretemp) nscore(1)
impute awaretemp H_Informed_F H_Infotest_F if latam==1, gen(aware)
*/
label variable E_Disc1Freq_F "First discussant"
label variable E_Disc2Freq_F "Second discussant"
label variable E_SpouTalk_F "Spouse"
label variable E_Disc1Know_F "First Alter"
label variable E_Disc2Know_F "Second Alter"
label variable E_SpouKnow_F "Spouse"

gen FamTalk = E_FamTalk_F
gen FriendTalk=E_FriendTalk_F
gen NeighTalk=E_NeighTalk_F
mvdecode FamTalk FriendTalk NeighTalk, mv(999)
recode FamTalk FriendTalk NeighTalk (995=0)
replace FamTalk =. if latam~=1
polychoricpca FamTalk FriendTalk NeighTalk, score(disc_freqtemp) nscore(1)
impute disc_freqtemp FamTalk FriendTalk NeighTalk if latam==1, gen(disc_freq)

replace E_Disc1Freq_F =. if A_A_CountryYear==12
replace E_Disc2Freq_F =. if A_A_CountryYear==12

svyset [pweight=A_A_WtCombined]
svy: tab E_Disc1Know_F if latam==1
svy: tab E_Disc2Know_F if latam==1
svy: tab E_SpouKnow_F if latam==1

label variable E_SpouTalk_F "Frequency of discussion with [discussant]"
label variable aware "MR's political knowledge"
label variable H_InterestCam_F "MR's campaign interest"
label variable disc_freq "MR's political discussion frequency"
label variable E_Disc1Freq_F "Frequency of discussion with [discussant]"
label variable E_Disc2Freq_F "Frequency of discussion with [discussant]"
label variable E_SpouKnow_F "Perceived political knowledge of [discussant]"
label variable E_Disc1Know_F "Perceived political knowledge of [discussant]"
label variable E_Disc2Know_F "Perceived political knowledge of [discussant]"

eststo clear
recode E_SpouTalk_F 0=0 1=1 2=1 3=1

*Table A.2
	*this are for reporting in the tables
*eststo: ologit E_SpouTalk_F E_SpouKnow_F H_InterestCam_F aware disc_freq i.A_A_CountryYear if latam==1 [pweight=A_A_WtCombined], cluster(A_A_CountryYear)
eststo: logit E_SpouTalk_F E_SpouKnow_F H_InterestCam_F aware disc_freq i.A_A_CountryYear if latam==1 [pweight=A_A_WtCombined], cluster(A_A_CountryYear)

	*this is for estimating predicted probs
*recode E_SpouTalk_F 0=0 1=1 2=2 3=2
*ologit E_SpouTalk_F E_SpouKnow_F H_InterestCam_F aware disc_freq i.A_A_CountryYear if latam==1 [pweight=A_A_WtCombined], cluster(A_A_CountryYear)
logit E_SpouTalk_F E_SpouKnow_F H_InterestCam_F aware disc_freq i.A_A_CountryYear if latam==1 [pweight=A_A_WtCombined], cluster(A_A_CountryYear)
margin, at((min) E_SpouKnow_F) at((max) E_SpouKnow_F) contrast(atcontrast(r)) vce(unconditional) atmeans
mat rb=r(b)
mat spouB=rb[1,1]
mat rV=r(V)
mat spouV=rV[1,1]

gen esample1=e(sample)
mean E_SpouTalk_F if esample1==1 [pweight=A_A_WtCombined]

	*this are for reporting in the tables
recode E_Disc1Freq_F 0=0 1=1 2=1 3=1
eststo: logit  E_Disc1Freq_F E_Disc1Know_F H_InterestCam_F aware disc_freq i.A_A_CountryYear if latam==1 [pweight=A_A_WtCombined], cluster(A_A_CountryYear)

	*this is for estimating predicted probs
*recode E_Disc1Freq_F 0=0 1=1 2=2 3=2
logit E_Disc1Freq_F E_Disc1Know_F H_InterestCam_F aware disc_freq i.A_A_CountryYear if latam==1 [pweight=A_A_WtCombined], cluster(A_A_CountryYear)
margin, at((min) E_Disc1Know_F ) at((max) E_Disc1Know_F ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat rb=r(b)
mat alter1B=rb[1,1]
mat rV=r(V)
mat alter1V=rV[1,1]

gen esample2=e(sample)
mean E_Disc1Freq_F if esample2==1 [pweight=A_A_WtCombined]

	*this are for reporting in the tables
recode E_Disc2Freq_F 0=0 1=1 2=1 3=1
eststo: logit E_Disc2Freq_F E_Disc2Know_F H_InterestCam_F aware disc_freq i.A_A_CountryYear if latam==1 [pweight=A_A_WtCombined], cluster(A_A_CountryYear)

	*this is for estimating predicted probs
*recode E_Disc2Freq_F 0=0 1=1 2=2 3=2
logit E_Disc2Freq_F E_Disc2Know_F H_InterestCam_F aware disc_freq i.A_A_CountryYear if latam==1 [pweight=A_A_WtCombined], cluster(A_A_CountryYear)
margin, at((min) E_Disc2Know_F ) at((max) E_Disc2Know_F ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat rb=r(b)
mat alter2B=rb[1,1]
mat rV=r(V)
mat alter2V=rV[1,1]

gen esample3=e(sample)
mean E_Disc2Freq_F  if esample3==1 [pweight=A_A_WtCombined]


mat Balter=[alter2B\alter1B\spouB]
mat Valter=[alter2V\alter1V\spouV]
mat list Balter

svmat Balter
svmat Valter
gen n=_n
replace n=. if n>3
gen alterlower=Balter-1.9645*(Valter^.5)
gen alterupper=Balter+1.9645*(Valter^.5)

gen Blab=round(Balter1,.001)

*Figure 2.7
twoway (rcap alterupper alterlower n if n==1, color(black) horizontal ) (scatter n Balter1  if n==1, mcolor(black) msym(t) mlab(Blab) mlabcolor(black) mlabpos(12)) ///
(rcap alterupper alterlower n if n==2, color(black) horizontal ) (scatter n Balter1  if n==2, mcolor(black) msym(S) mlab(Blab) mlabcolor(black) mlabpos(12)) ///
(rcap alterupper alterlower n if n==3, color(black) horizontal ) (scatter n Balter1  if n==3, mcolor(black) mlab(Blab) mlabcolor(black) mlabpos(12)) , ///
xtitle("{bf:Model-Predicted Change in Predicted Probability}" "(upon changing alter from politically uninformed to very well-informed)", color(black)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("") ///
title("{bf:Dependent Variable: Social Alter Selected as Political Alter}", size(medsmall) color(black)) ytitle("{bf:Which Alter?}" " ") ///
legend(off) ///
ylab(3 "Spouse" 2 "Alter 2" 1 "Alter 3" , angle(horizontal) )  yscale(range(.75 3.25)) xscale(range(0 .7)) ///
xlab(0 .25 .5 .75) text(1 .05 "{it:N}=2,312") text(2 .05 "{it:N}=4,669") text(3 .05 "{it:N}=3,251")

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Figures\CNEPknow.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_7.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_7.pdf", as(pdf) replace 

*esttab est1 est2 est3 using table27.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Main Respondent’s Frequency of Discussion with a Discussant by the Perceived Political Knowledge of that Discussant in Four Latin American Countries: Change in Predicted Probabilities from Three Ordered Logit Models)

logit E_SpouTalk_F i.A_A_CountryYear if latam==1 [pweight=A_A_WtCombined], cluster(A_A_CountryYear)


drop n


*DISAGREEMENT STUFF

gen agree_spouse_restrictive=0 if H_VoteWhichRecent_F~=. & E_SpouVote2_F ~=.
recode agree_spouse_restrictive 0=1 if H_VoteWhichRecent_F==E_SpouVote2_F
replace agree_spouse_restrictive=. if H_VoteWhichRecent_F>30
replace agree_spouse_restrictive=. if E_SpouVote2_F>50
replace agree_spouse_restrictive=. if E_SpouVote2_F==0

gen agree_disc1_restrictive=0 if H_VoteWhichRecent_F~=. & E_Disc1Part_F ~=.
recode agree_disc1_restrictive 0=1 if H_VoteWhichRecent_F == E_Disc1Part_F
replace agree_disc1_restrictive=. if H_VoteWhichRecent_F>30
replace agree_disc1_restrictive=. if E_Disc1Part_F>50
replace agree_disc1_restrictive=. if E_Disc1Part_F==0

gen agree_disc2_restrictive=0 if H_VoteWhichRecent_F~=. & E_Disc2Part_F ~=.
recode agree_disc2_restrictive 0=1 if H_VoteWhichRecent_F == E_Disc2Part_F
replace agree_disc2_restrictive=. if H_VoteWhichRecent_F>30
replace agree_disc2_restrictive=. if E_Disc2Part_F>50
replace agree_disc2_restrictive=. if E_Disc2Part_F==0

gen agree_paper_restrictive=0 if H_VoteWhichRecent_F~=. & D_Pap1Part_F  ~=.
recode agree_paper_restrictive 0=1 if H_VoteWhichRecent_F == D_Pap1Part_F 
replace agree_paper_restrictive=. if H_VoteWhichRecent_F>30
replace agree_paper_restrictive=. if D_Pap1Part_F  >50
replace agree_paper_restrictive=. if D_Pap1Part_F  ==0

gen agree_radio_restrictive=0 if H_VoteWhichRecent_F~=. & D_Rad1Part_F  ~=.
recode agree_radio_restrictive 0=1 if H_VoteWhichRecent_F == D_Rad1Part_F
replace agree_radio_restrictive=. if H_VoteWhichRecent_F>30
replace agree_radio_restrictive=. if D_Rad1Part_F  >50
replace agree_radio_restrictive=. if D_Rad1Part_F  ==0

gen agree_TV_restrictive=0 if H_VoteWhichRecent_F~=. & D_TV1Part_F~=.
recode agree_TV_restrictive 0=1 if H_VoteWhichRecent_F == D_TV1Part_F
replace agree_TV_restrictive=. if H_VoteWhichRecent_F>30
replace agree_TV_restrictive=. if D_TV1Part_F>50
replace agree_TV_restrictive=. if D_TV1Part_F==0

recode H_VoteWhichRecent_F 993=. 995=. 999=.
tab H_VoteWhichRecent_F, gen(vote_party)

recode D_Pap1Part_F (0=0) (.=.) (995=0) (999=0) (else=1), gen(PapRecog)
recode D_Rad1Part_F (0=0) (.=.) (995=0) (999=0) (else=1), gen(RadRecog)
recode D_TV1Part_F (0=0) (.=.) (995=0) (999=0) (else=1), gen(TVRecog)

recode E_Disc1Part_F (0=0) (.=.) (995=0) (999=0) (else=1), gen(Disc1Recog)
recode E_Disc2Part_F (0=0) (.=.) (995=0) (999=0) (else=1), gen(Disc2Recog)
recode E_SpouVote2_F (0=0) (.=.) (995=0) (999=0) (else=1), gen(SpouRecog)

	*gets number of country years
bysort A_A_CountryYear: summ SpouRecog Disc1Recog  Disc2Recog D_Pap1Part_F D_Rad1Part_F  D_TV1Part_F

svy: tab SpouRecog if latam==1 & l_married==2 
mat eb=e(b)
mat spoula=eb[1,2]

svy: tab Disc1Recog if latam==1 & e_disc1<9
mat eb=e(b)
mat disc1la=eb[1,2]

svy: tab Disc2Recog if latam==1 & e_disc2<9
mat eb=e(b)
mat disc2la=eb[1,2]

svy: tab TVRecog if latam==1 & d_tv1freq_f>0 & d_tv1freq_f<8
mat eb=e(b)
mat tvla=eb[1,2]

svy: tab PapRecog if latam==1 & d_pap1freq_f>0 & d_pap1freq_f<8
mat eb=e(b)
mat papla=eb[1,2]

svy: tab RadRecog if latam==1  & d_rad1freq_f>0 & d_rad1freq_f<8
mat eb=e(b)
mat radla=eb[1,2]

svy: tab SpouRecog if latam~=1 & l_married==2
mat eb=e(b)
mat spou=eb[1,2]

svy: tab Disc1Recog if latam~=1 & e_disc1<9
mat eb=e(b)
mat disc1=eb[1,2]

svy: tab Disc2Recog if latam~=1 & e_disc2<9
mat eb=e(b)
mat disc2=eb[1,2]

svy: tab TVRecog if latam~=1 & d_tv1freq_f>0 & d_tv1freq_f<8
mat eb=e(b)
mat tv=eb[1,2]

svy: tab PapRecog if latam~=1 & d_pap1freq_f>0 & d_pap1freq_f<8
mat eb=e(b)
mat pap=eb[1,2]

svy: tab RadRecog if latam~=1 & d_rad1freq_f>0 & d_rad1freq_f<8
mat eb=e(b)
mat rad=eb[1,2]

mat bargraphla=[spoula\.\.\disc1la\.\.\disc2la\.\.\tvla\.\.\papla\.\.\radla]
svmat bargraphla
mat bargraph=[.\spou\.\.\disc1\.\.\disc2\.\.\tv\.\.\pap\.\.\rad]
svmat bargraph

gen n=_n
replace n=. if n>18

*Figure 2.10
twoway (bar bargraphla1 n if n<18, color(black) lwidth(none)) ///
(bar bargraph1 n if n<18, color(gs8) lwidth(none)) , ///
yscale(range(0 1)) ylab(0 "0%" .2 "20%" .4 "40%" .6 "60%" .8 "80%" 1 "100%") ytitle("{bf:Percentage of Respondents who Give an}" "{bf:Estimate of the Intermediary's Political Bias}") ///
xscale(range(0 16)) graphregion(color(white)) ttitle("{bf:Political Intermediary}") ///
plotregion(lstyle(yxline) lcolor(black))  legend(order(1 "Latin America" 2 "Other")) ///
xlab(1.5 "Spouse" 4.5 "Alter 2" 7.5 "Alter 3" 10.5 "TV News" 13.5 "Paper" 16.5 "Radio") xtitle("")
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Figures\CNEPRecognize.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_10.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_10.pdf", as(pdf) replace 


preserve
collapse (mean) agree_spouse_restrictive agree_disc1_restrictive agree_disc2_restrictive agree_paper_restrictive agree_radio_restrictive agree_TV_restrictive vote_party* PapRecog RadRecog TVRecog Disc1Recog Disc2Recog SpouRecog [pweight=L_WtWithin], by(A_A_CountryYear)
merge 1:1  A_A_CountryYear using "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Data\effective num parties.dta"

recode A_A_CountryYear 1=1 3=1 4=1 19=1 30=1 31=1 32=1 33=1 37=1 56=1 else=0, gen(latam)
gen label="ARG07" if A_A_CountryYear==1
replace label="BGR96" if A_A_CountryYear==2
replace label="CHL93" if A_A_CountryYear==3 
replace label="CHL00" if A_A_CountryYear==4
replace label="CHN08" if A_A_CountryYear==5
replace label="GRC96" if A_A_CountryYear==7 
replace label="GRC04" if A_A_CountryYear==8 
replace label="HKG98" if A_A_CountryYear==9 
replace label="HUN98" if A_A_CountryYear==11
replace label="HUN06" if A_A_CountryYear==12
replace label="IND99" if A_A_CountryYear==13
replace label="IND04" if A_A_CountryYear==14
replace label="IND09" if A_A_CountryYear==15
replace label="ITA96" if A_A_CountryYear==16
replace label="ITA06" if A_A_CountryYear==17
replace label="MEX06" if A_A_CountryYear==19
replace label="MOZ04" if A_A_CountryYear==20
replace label="PRT05" if A_A_CountryYear==21
replace label="ZAF04" if A_A_CountryYear==22
replace label="ZAF09" if A_A_CountryYear==23
replace label="ESP93" if A_A_CountryYear==24
replace label="ESP04" if A_A_CountryYear==25
replace label="TWN04" if A_A_CountryYear==26
replace label="GBR92" if A_A_CountryYear==27
replace label="USA92" if A_A_CountryYear==28
replace label="USA04" if A_A_CountryYear==29
replace label="URY94" if A_A_CountryYear==30
replace label="URY04" if A_A_CountryYear==31
replace label="DOM10" if A_A_CountryYear==32
replace label="MEX12" if A_A_CountryYear==33
replace label="USA12" if A_A_CountryYear==34
replace label="ITA13" if A_A_CountryYear==35
replace label="KEN13" if A_A_CountryYear==36
replace label="COL14" if A_A_CountryYear==37
replace label="ESP15" if A_A_CountryYear==44
replace label="ESP11" if A_A_CountryYear==48
replace label="MEX18" if A_A_CountryYear==56

local i=1
while `i'<=19 {
gen vote_partysq`i'=vote_party`i'^2
local i=`i'+1 
}

egen frag=rowtotal(vote_partysq*)
gen eff_party=1/frag

*this gets rid of Italy 96, which is serious outlier
*replace eff_party=. if eff_party>5.6
drop if eff_party==.

gen xaxis=_n*.0945+1.86
gen line=1/xaxis
gen xarrow1s = 3
gen yarrow1s = .27
gen xarrow1e = 3.05
gen yarrow1e = .327
gen xarrow2s = 4
gen yarrow2s = .82
gen xarrow2e = 4
gen yarrow2e = .77
gen xarrow3s = 4
gen yarrow3s = .735
gen xarrow3e = 4
gen yarrow3e = .685
gen xarrow4s = 4
gen yarrow4s = .64
gen xarrow4e = 4
gen yarrow4e = .59

		*Spouse
gen eff_partysq=eff_no_parties^2
reg agree_spouse_restrictive  eff_no_parties eff_partysq
predict xb, xb
replace line=. if xaxis>5.3 & xaxis<2
corr agree_spouse_restrictive  eff_party

*FIGURE 2.9, Frame A
twoway (scatter agree_spouse_restrictive  eff_no_parties if latam==0, msym(i) mlab(label) mlabpos(0) mlabcolor(gs8) mlabsize(small)) ///
(scatter agree_spouse_restrictive eff_no_parties if latam==1, msym(i) mlab(label) mlabpos(0) mlabcolor(black) mlabsize(small)) ///
(scatter line xaxis if xaxis<5.25, connect(l) sort msym(i) lcolor(black) lpattern(dash)) (scatter xb eff_no_parties, connect(l) sort msym(i) lcolor(black)) ///
(pcarrow yarrow2s xarrow2s yarrow2e xarrow2e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow1s xarrow1s yarrow1e xarrow1e, lcolor(black) mcolor(black)) , ///
ytitle("{bf:Percentage of Spousal}" "{bf:Dyads that Agree}", size(medlarge) axis(1) color(black)) ///
yscale(range(0 1)) ytick(0(.25)1) ylab(0 "0%" .25 "25%" .5 "50%" .75 "75%" 1 "100%", labsize(medium))  xscale(range(1.8 5.5)) xlab(,labsize(medium)) ///
xtitle("{bf:Effective Number of Parties/Candidates}", size(medlarge)) graphregion(color(white)) ///
plotregion(lstyle(yxline) lcolor(black)) legend(off) ///
note("{it:r} = -.68", color(black) size(medium)) text(.2 2.9 "Expected relationship if spouses" "paired at random" "and no influence occurred", size(medsmall)) ///
text(.87 4 "Observed" "relationship", size(medsmall))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Figures\CNEPAgreeSpouse.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_9A.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_9A.pdf", as(pdf) replace 

		*Disc1
reg agree_disc1_restrictive eff_no_parties eff_partysq
predict xb2, xb
corr agree_disc1_restrictive  eff_no_parties

*FIGURE 2.9, Frame B
twoway (scatter agree_disc1_restrictive eff_no_parties if latam==0, msym(i) mlab(label) mlabpos(0) mlabcolor(gs8) mlabsize(small)) ///
(scatter agree_disc1_restrictive eff_no_parties if latam==1, msym(i) mlab(label) mlabpos(0) mlabcolor(black) mlabsize(small)) ///
(scatter line xaxis if xaxis<5.25, connect(l) sort msym(i) lcolor(black) lpattern(dash)) (scatter xb2 eff_no_parties if eff_no_parties<6, connect(l) sort msym(i) lcolor(black)) ///
(pcarrow yarrow3s xarrow3s yarrow3e xarrow3e , lcolor(black) mcolor(black)) ///
(pcarrow yarrow1s xarrow1s yarrow1e xarrow1e, lcolor(black) mcolor(black)) , ///
ytitle("{bf:Percentage of Ego↔Alter2}" "{bf:Dyads that Agree}", size(medlarge) axis(1) color(black)) ///
yscale(range(0 1)) ytick(0(.25)1) ylab(0 "0%" .25 "25%" .5 "50%" .75 "75%" 1 "100%", labsize(medium)) xscale(range(1.8 5.5)) xlab(,labsize(medium)) ///
xtitle("{bf:Effective Number of Parties/Candidates}", size(medlarge)) graphregion(color(white)) ///
plotregion(lstyle(yxline) lcolor(black)) legend(off) ///
note("{it:r} = -.57", color(black) size(medium)) text(.2 2.9 "Expected relationship if discussion" "partners paired at random" "and no influence occurred", size(medsmall)) ///
text(.7875 4 "Observed" "relationship", size(medsmall))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Figures\CNEPAgreeDisc1.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_9B.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_9B.pdf", as(pdf) replace 

		*Disc2
reg agree_disc2_restrictive eff_no_parties eff_partysq
predict xb3, xb
corr agree_disc2_restrictive  eff_no_parties

*FIGURE 2.9, Frame C
twoway (scatter agree_disc2_restrictive eff_no_parties if latam==0, msym(i) mlab(label) mlabpos(0) mlabcolor(gs8) mlabsize(small)) ///
(scatter agree_disc2_restrictive eff_no_parties if latam==1, msym(i) mlab(label) mlabpos(0) mlabcolor(black) mlabsize(small)) ///
(scatter line xaxis if xaxis<5.25, connect(l) sort msym(i) lcolor(black) lpattern(dash)) (scatter xb3 eff_no_parties if eff_no_parties<6, connect(l) sort msym(i) lcolor(black)) ///
(pcarrow yarrow4s xarrow4s yarrow4e xarrow4e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow1s xarrow1s yarrow1e xarrow1e, lcolor(black) mcolor(black)) , ///
ytitle("{bf:Percentage of Ego↔Alter3}" "{bf:Dyads that Agree}", size(medlarge) axis(1) color(black)) ///
yscale(range(0 1)) ytick(0(.25)1) ylab(0 "0%" .25 "25%" .5 "50%" .75 "75%" 1 "100%", labsize(medium)) xscale(range(1.8 5.5)) xlab(,labsize(medium)) ///
xtitle("{bf:Effective Number of Parties/Candidates}", size(medlarge)) graphregion(color(white)) ///
plotregion(lstyle(yxline) lcolor(black)) legend(off) ///
note("{it:r} = -.69", color(black) size(medium)) text(.20 2.9 "Expected relationship if discussion" "partners paired at random" "and no influence occurred", size(medsmall)) ///
text(.69 4 "Observed" "relationship", size(medsmall))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Figures\CNEPAgreeDisc2.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_9C.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_9C.pdf", as(pdf) replace 

*These next three aren't very useful because N's are so low, and i didn't update
		*Radio
reg agree_radio_restrictive eff_no_parties eff_partysq
predict xb4, xb
corr agree_radio_restrictive eff_no_parties

gen xarrow5s = .
gen yarrow5s = .
gen xarrow5e = .
gen yarrow5e = .

replace xarrow5s = 1.97
replace yarrow5s = .837
replace xarrow5e = 1.76
replace yarrow5e = .76

gen xarrow6s = .
gen yarrow6s = .
gen xarrow6e = .
gen yarrow6e = .

replace xarrow6s = 1.75
replace yarrow6s = .33
replace xarrow6e = 2.03
replace yarrow6e = .49

twoway (scatter agree_radio_restrictive eff_no_parties if latam==0, msym(i) mlab(label) mlabpos(0) mlabcolor(gs8) mlabsize(small)) ///
(scatter agree_radio_restrictive eff_no_parties if latam==1, msym(i) mlab(label) mlabpos(0) mlabcolor(black) mlabsize(small)) ///
(scatter line xaxis if xaxis<3.5, connect(l) sort msym(i) lcolor(black) lpattern(dash)) (scatter xb4 eff_no_parties if eff_no_parties<3.5, connect(l) sort msym(i) lcolor(black)) ///
(pcarrow yarrow5s xarrow5s yarrow5e xarrow5e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow6s xarrow6s yarrow6e xarrow6e, lcolor(black) mcolor(black)) , ///
ytitle("{bf:Proportion of Respondents who Agree Politically}" "{bf:with their Main Radio News Program}", axis(1) color(black)) ///
yscale(range(0 1)) ytick(0(.25)1) ylab(0(.25)1,) ///
xtitle("{bf:Effective Number of Parties/Candidates}") graphregion(color(white)) ///
plotregion(lstyle(yxline) lcolor(black)) legend(off) ///
note("{it:r} = -.81", color(black) size(medsmall)) text(.3 1.75 "Expected relationship if individuals" "paired to radio programs at random", size(small)) ///
text(.88 2 "Observed" "relationship", size(small))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Figures\CNEPAgreeRadio.tif", as(tif) replace

		*Paper
reg agree_paper_restrictive eff_no_parties eff_partysq
predict xb5, xb
corr agree_paper_restrictive eff_no_parties
gen xarrow7s = .
gen yarrow7s = .
gen xarrow7e = .
gen yarrow7e = .
gen xarrow8s = .
gen yarrow8s = .
gen xarrow8e = .
gen yarrow8e = .

replace xarrow7s = 2
replace yarrow7s = .71
replace xarrow7e = 1.85
replace yarrow7e = .63

replace xarrow8s = 3.7
replace yarrow8s = .18
replace xarrow8e = 3.7
replace yarrow8e = .27

twoway (scatter agree_paper_restrictive eff_no_parties if latam==0, msym(i) mlab(label) mlabpos(0) mlabcolor(gs8) mlabsize(small)) ///
(scatter agree_paper_restrictive eff_no_parties if latam==1, msym(i) mlab(label) mlabpos(0) mlabcolor(black) mlabsize(small)) ///
(scatter line xaxis if xaxis<4.5, connect(l) sort msym(i) lcolor(black) lpattern(dash)) (scatter xb5 eff_no_parties if eff_no_parties<4.5, connect(l) sort msym(i) lcolor(black)) ///
(pcarrow yarrow7s xarrow7s yarrow7e xarrow7e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow8s xarrow8s yarrow8e xarrow8e, lcolor(black) mcolor(black)) , ///
ytitle("{bf:Proportion of Respondents who Agree Politically}" "{bf:with their Main Newspaper Source}", axis(1) color(black)) ///
yscale(range(0 1)) ytick(0(.25)1) ylab(0(.25)1,) ///
xtitle("{bf:Effective Number of Parties/Candidates}") graphregion(color(white)) ///
plotregion(lstyle(yxline) lcolor(black)) legend(off) ///
note("{it:r} = -.47", color(black) size(medsmall)) text(.15 3.7 "Expected relationship if individuals" "paired to newspapers at random", size(small)) ///
text(.75 2 "Observed" "relationship", size(small))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Figures\CNEPAgreePaper.tif", as(tif) replace

		*TV
reg agree_TV_restrictive eff_no_parties eff_partysq
predict xb6, xb
corr agree_TV_restrictive eff_no_parties

gen xarrow9s = .
gen yarrow9s = .
gen xarrow9e = .
gen yarrow9e = .
gen xarrow10s = .
gen yarrow10s = .
gen xarrow10e = .
gen yarrow10e = .

replace xarrow9s = 1.6
replace yarrow9s = .84
replace xarrow9e = 1.43
replace yarrow9e = .77

replace xarrow10s = 3.9
replace yarrow10s = .18
replace xarrow10e = 3.9
replace yarrow10e = .25

twoway (scatter agree_TV_restrictive eff_no_parties if latam==0, msym(i) mlab(label) mlabpos(0) mlabcolor(gs8) mlabsize(small)) ///
(scatter agree_TV_restrictive eff_no_parties if latam==1, msym(i) mlab(label) mlabpos(0) mlabcolor(black) mlabsize(small)) ///
(scatter line xaxis if xaxis<4.5, connect(l) sort msym(i) lcolor(black) lpattern(dash)) (scatter xb6 eff_no_parties if eff_no_parties<4.5, connect(l) sort msym(i) lcolor(black)) ///
(pcarrow yarrow9s xarrow9s yarrow9e xarrow9e, lcolor(black) mcolor(black)) ///
(pcarrow yarrow10s xarrow10s yarrow10e xarrow10e, lcolor(black) mcolor(black)) , ///
ytitle("{bf:Proportion of Respondents who Agree Politically}" "{bf:with their Main TV News Program}", axis(1) color(black)) ///
yscale(range(0 1)) ytick(0(.25)1) ylab(0(.25)1,) ///
xtitle("{bf:Effective Number of Parties/Candidates}") graphregion(color(white)) ///
plotregion(lstyle(yxline) lcolor(black)) legend(off) ///
note("{it:r} = -.58", color(black) size(medsmall)) text(.15 3.9 "Expected relationship if individuals" "paired to TV programs at random", size(small)) ///
text(.88 1.6 "Observed" "relationship", size(small))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Figures\CNEPAgreeTV.tif", as(tif) replace

restore
*End Disagreement Stuff
*------------------------------------
tab1 E_FriendTalk_F E_FamTalk_F  E_WorkTalk_F E_NeighTalk_F , m

recode E_FriendTalk_F E_FamTalk_F  E_WorkTalk_F E_NeighTalk_F (995=0)
mvdecode E_FriendTalk_F E_FamTalk_F  E_WorkTalk_F E_NeighTalk_F L_Housing_F L_Neighborhood_F , mv(999)

gen talkatall=1 if E_FriendTalk_F==1 | E_FriendTalk_F==2 | E_FriendTalk_F==3 | E_FamTalk_F==1 |  E_FamTalk_F==2 |  E_FamTalk_F==3 |  E_NeighTalk_F==1 |  E_NeighTalk_F==2 |  E_NeighTalk_F==3
replace talkatall =0 if E_FriendTalk_F==0 & E_FamTalk_F==0 & E_NeighTalk_F==0

*replace talkatall=. if A_A_CountryYear==13
tab1 E_FriendTalk_F E_FamTalk_F  E_WorkTalk_F E_NeighTalk_F , m

*---------------------------------------------------------------------------------
*STRATIFICATION STUFF: IMPORTANT TO PRESERVE DATASET HERE
preserve
keep if A_A_CountryYear==1 | A_A_CountryYear==3 | A_A_CountryYear==4 | A_A_CountryYear==19 | A_A_CountryYear==30 | A_A_CountryYear==31 | A_A_CountryYear==32 | A_A_CountryYear==33 | A_A_CountryYear==37 | A_A_CountryYear==56

*SES
drop wealth
gen wealth=ln(L_Income_F+1)
impute wealth L_Housing_F L_Neighborhood_F, gen(wealth1)

drop educ
gen educ=ln(L_Education_F+1)

recode L_Gender_F 2=0, gen(male)

* Conversation Frequency DV
*doing this with factor instead of polychoric b/c polychoric won't take the if command
pca E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==1 , com(1)  
predict converse1temp if A_A_CountryYear==1
impute converse1temp E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==1, gen(converse1)

pca E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==3 , com(1)  
predict converse3temp if A_A_CountryYear==3
impute converse3temp E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==3, gen(converse3)

pca E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==4 , com(1)  
predict converse4temp if A_A_CountryYear==4
impute converse4temp E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==4, gen(converse4)

pca E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==19, com(1)  
predict converse19temp if A_A_CountryYear==19
impute converse19temp E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==19, gen(converse19)

pca E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==30, com(1)  
predict converse30temp if A_A_CountryYear==30
impute converse30temp E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==30, gen(converse30)

pca E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==31, com(1)  
predict converse31temp if A_A_CountryYear==31
impute converse31temp E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==31, gen(converse31)

pca E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==32, com(1)  
predict converse32temp if A_A_CountryYear==32
impute converse32temp E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==32, gen(converse32)

pca E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==33, com(1)  
predict converse33temp if A_A_CountryYear==33
impute converse33temp E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==33, gen(converse33)

pca E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==37, com(1)  
predict converse37temp if A_A_CountryYear==37
impute converse37temp E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==37, gen(converse37)

pca E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==56, com(1)  
predict converse56temp if A_A_CountryYear==56
impute converse56temp E_FriendTalk_F E_FamTalk_F E_NeighTalk_F if A_A_CountryYear==56, gen(converse56)

egen conversemean1=std(converse1)
egen conversemean3=std(converse3)
egen conversemean4=std(converse4)
egen conversemean19=std(converse19)
egen conversemean30=std(converse30)
egen conversemean31=std(converse31)
egen conversemean32=std(converse32)
egen conversemean33=std(converse33)
egen conversemean37=std(converse37)
egen conversemean56=std(converse56)

*Party contact DV
egen partycontactface=anycount(I_Gov1Pers_F I_Gov2Pers_F I_Gov3Pers_F I_Gov4Pers_F I_Gov5Pers_F I_Opp1Pers_F I_Opp2Pers_F I_Opp3Pers_F I_Opp4Pers_F I_Opp5Pers_F I_Opp6Pers_F), values(1)
*egen partycontactphone=anycount(I_Gov1Tele_F I_Gov2Tele_F I_Gov3Tele_F I_Gov4Tele_F I_Gov5Tele_F I_Opp1Tele_F I_Opp2Tele_F I_Opp3Tele_F I_Opp4Tele_F I_Opp5Tele_F I_Opp6Tele_F), values(1)
*gen partycontacttemp=partycontactface+partycontactphone
recode partycontactface 0=0 1/9=1, gen(partycontact)

egen partycontact3=std(partycontact) if A_A_CountryYear==3
egen partycontact19=std(partycontact) if A_A_CountryYear==19
egen partycontact30=std(partycontact) if A_A_CountryYear==30
egen partycontact32=std(partycontact) if A_A_CountryYear==32
egen partycontact33=std(partycontact) if A_A_CountryYear==33
egen partycontact37=std(partycontact) if A_A_CountryYear==37
egen partycontact56=std(partycontact) if A_A_CountryYear==56

*Media Exposure
	*mags and internet are missing for some years
recode D_CamPaper_F 0=0 1=.5 2=1.5 3=3.5 4=6 999=., gen(paper)
recode D_CamTV_F 0=0 1=.5 2=1.5 3=3.5 4=6 999=., gen(tv)
recode D_CamRadio_F  0=0 1=.5 2=1.5 3=3.5 4=6 999=., gen(radio)

*doing this with factor instead of polychoric b/c polychoric won't take the if command
pca paper tv radio if A_A_CountryYear==1 , com(1)  
predict media1temp if A_A_CountryYear==1 
impute media1temp paper tv radio if A_A_CountryYear==1, gen(mediaattention1)

pca paper tv radio if A_A_CountryYear==3, com(1)  
predict media3temp if A_A_CountryYear==3
impute media3temp paper tv radio if A_A_CountryYear==3, gen(mediaattention3)

pca paper tv radio if A_A_CountryYear==4, com(1)  
predict media4temp if A_A_CountryYear==4
impute media4temp paper tv radio if A_A_CountryYear==4, gen(mediaattention4)

pca paper tv radio if A_A_CountryYear==19, com(1)  
predict media19temp if A_A_CountryYear==19
impute media19temp paper tv radio if A_A_CountryYear==19, gen(mediaattention19)

pca paper tv radio if A_A_CountryYear==30, com(1)  
predict media130temp if A_A_CountryYear==30
impute media130temp paper tv radio if A_A_CountryYear==30, gen(mediaattention30)

gen mediaattention31 = tv if A_A_CountryYear==31

pca paper tv radio if A_A_CountryYear==32, com(1)  
predict media132temp if A_A_CountryYear==32
impute media132temp paper tv radio if A_A_CountryYear==32, gen(mediaattention32)

pca paper tv radio if A_A_CountryYear==33, com(1)  
predict media133temp if A_A_CountryYear==33
impute media133temp paper tv radio if A_A_CountryYear==33, gen(mediaattention33)

pca paper tv radio if A_A_CountryYear==37, com(1)  
predict media137temp if A_A_CountryYear==37
impute media137temp paper tv radio if A_A_CountryYear==37, gen(mediaattention37)

pca paper tv radio if A_A_CountryYear==56, com(1)  
predict media156temp if A_A_CountryYear==56
impute media156temp paper tv radio if A_A_CountryYear==56, gen(mediaattention56)

egen mediaattention1_std=std(mediaattention1)
egen mediaattention3_std=std(mediaattention3)
egen mediaattention4_std=std(mediaattention4)
egen mediaattention19_std=std(mediaattention19)
egen mediaattention30_std=std(mediaattention30)
egen mediaattention31_std=std(mediaattention31)
egen mediaattention32_std=std(mediaattention32)
egen mediaattention33_std=std(mediaattention33)
egen mediaattention37_std=std(mediaattention37)
egen mediaattention56_std=std(mediaattention56)

egen partycontact_std=rowmean(partycontact3 partycontact19 partycontact30 partycontact32 partycontact33 partycontact37 partycontact56)
egen conversemean_std=rowmean(conversemean56 conversemean37 conversemean33 conversemean32 conversemean31 conversemean30 conversemean19 conversemean4 conversemean3 conversemean1)
egen mediaattention_std=rowmean(mediaattention56_std mediaattention37_std mediaattention33_std mediaattention32_std mediaattention31_std mediaattention30_std mediaattention19_std mediaattention4_std mediaattention3_std mediaattention1_std)

label variable wealth1 "Wealth"
label variable educ "Education level"
label variable male "Male"
label variable conversemean_std "Frequency of political discussion"
label variable partycontact_std "Contacted by a party"
label variable mediaattention_std "Media exposure"

*Regs and plots
		*-----------------First one: Chile and Uruguay
*Table A.22
eststo clear
*CHL93
eststo: reg conversemean_std wealth1 educ male if A_A_CountryYear==3 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealtho93=r(b)
mat vwealtho93=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexo93=r(b)
mat vsexo93=r(V)

eststo: reg mediaattention_std wealth1 educ male if A_A_CountryYear==3 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthm93=r(b)
mat vwealthm93=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexm93=r(b)
mat vsexm93=r(V)

eststo: reg partycontact_std wealth1 educ male if A_A_CountryYear==3 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthp93=r(b)
mat vwealthp93=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexp93=r(b)
mat vsexp93=r(V)

*CHL00
eststo: reg conversemean_std wealth1 educ male if A_A_CountryYear==4 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealtho00=r(b)
mat vwealtho00=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexo00=r(b)
mat vsexo00=r(V)

eststo: reg mediaattention_std wealth1 educ male if A_A_CountryYear==4 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthm00=r(b)
mat vwealthm00=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexm00=r(b)
mat vsexm00=r(V)

mat bwealthp00=.
mat vwealthp00=.
mat bsexp00=.
mat vsexp00=.

*URY94
eststo: reg conversemean_std wealth1 educ male if A_A_CountryYear==30 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealtho94=r(b)
mat vwealtho94=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexo94=r(b)
mat vsexo94=r(V)

eststo: reg mediaattention_std wealth1 educ male if A_A_CountryYear==30 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthm94=r(b)
mat vwealthm94=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexm94=r(b)
mat vsexm94=r(V)

eststo: reg partycontact_std wealth1 educ male if A_A_CountryYear==30 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthp94=r(b)
mat vwealthp94=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexp94=r(b)
mat vsexp94=r(V)

*URY04
eststo: reg conversemean_std wealth1 educ male if A_A_CountryYear==31 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealtho04=r(b)
mat vwealtho04=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexo04=r(b)
mat vsexo04=r(V)

eststo: reg mediaattention_std wealth1 educ male if A_A_CountryYear==31 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthm04=r(b)
mat vwealthm04=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexm04=r(b)
mat vsexm04=r(V)

mat bwealthp04=.
mat vwealthp04=.
mat bsexp04=.
mat vsexp04=.

*COL14
eststo: reg conversemean_std wealth1 educ male if A_A_CountryYear==37 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealtho14=r(b)
mat vwealtho14=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexo14=r(b)
mat vsexo14=r(V)

eststo: reg mediaattention_std wealth1 educ male if A_A_CountryYear==37 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthm14=r(b)
mat vwealthm14=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexm14=r(b)
mat vsexm14=r(V)

eststo: reg partycontact_std wealth1 educ male if A_A_CountryYear==37 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthp14=r(b)
mat vwealthp14=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexp14=r(b)
mat vsexp14=r(V)

*for appendix
cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 normative\Output\"
esttab est1 est2 est3 est4 est5 using CHLstrat.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Table: Correlates of Intermediation Exposure, Chile)
esttab est6 est7 est8 est9 est10 using URYstrat.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Table: Correlates of Intermediation Exposure, Uruguay)
esttab est11 est12 est13 using COLstrat.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Table: Correlates of Intermediation Exposure, Colombia)

mat Bwealth=[bwealthp14\bwealthp04\bwealthp94\bwealthp00\bwealthp93\bwealthm14\bwealthm04\bwealthm94\bwealthm00\bwealthm93\bwealtho14\bwealtho04\bwealtho94\bwealtho00\bwealtho93]
mat Bsex=[bsexp14\bsexp04\bsexp94\bsexp00\bsexp93\bsexm14\bsexm04\bsexm94\bsexm00\bsexm93\bsexo14\bsexo04\bsexo94\bsexo00\bsexo93]

mat Vwealth=[vwealthp14\vwealthp04\vwealthp94\vwealthp00\vwealthp93\vwealthm14\vwealthm04\vwealthm94\vwealthm00\vwealthm93\vwealtho14\vwealtho04\vwealtho94\vwealtho00\vwealtho93]
mat Vsex=[vsexp14\vsexp04\vsexp94\vsexp00\vsexp93\vsexm14\vsexm04\vsexm94\vsexm00\vsexm93\vsexo14\vsexo04\vsexo94\vsexo00\vsexo93]

svmat Bwealth 
svmat Bsex 
svmat Vwealth 
svmat Vsex 
gen nwealth=_n+.125
gen nsex=_n-.125
replace n=_n
gen wealthupper=Bwealth+1.9645*(Vwealth^.5)
gen wealthlower=Bwealth-1.9645*(Vwealth^.5)
gen sexupper=Bsex+1.9645*(Vsex^.5)
gen sexlower=Bsex-1.9645*(Vsex^.5)

*Figure 8.2A
*no legend
twoway (rcap wealthupper wealthlower nwealth if n<16, color(black) horizontal ) (scatter nwealth Bwealth if n<16, mcolor(black)) ///
(rcap sexupper sexlower nsex if n<16, color(black) horizontal ) (scatter nsex Bsex if n<16, mcolor(black) msymbol(S) msize(medsmall)), ///
xtitle("{bf:Model-Predicted Change in DV}" "(upon changing SES or gender from min to max)", size(medium)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("{bf:Dependent Variables(DV):}" "{bf:Exposure to Political Intermediaries}" "(Country and Year)", size(medium))  ///
legend(off) ///
xline(0, lcolor(black)) ymlab(5 "CHL93" 4 "CHL00" 3 "URY94" 2 "URY04" 1 "COL14" 10 "CHL93" 9 "CHL00" 8 "URY94" 7 "URY04" 6 "COL14" 15 "CHL93" 14 "CHL00" 13 "URY94" 12 "URY04" 11 "COL14", angle(horizontal) labsize(medsmall)) ///
ylab(13 "Freq. of pol. disc." 8 "Media exposure" 3 "Contact by party" , angle(horizontal) notick labgap(14) labsize(medsmall)) ///
ytick(5.5 10.5, tlength(40)) ylab(,nogrid) xscale(range(-1.5 2.3)) xlab(-1.5 -1 -.5 0 .5 1 1.5 2.0, grid) yline(5.5 10.5, lcolor(black))
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Figures\Chapter 8 Panel A.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_2A.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_2A.pdf", as(pdf) replace 

*with legend
twoway (rcap wealthupper wealthlower nwealth if n<13, color(black) horizontal ) (scatter nwealth Bwealth if n<13, mcolor(black)) ///
(rcap sexupper sexlower nsex if n<13, color(black) horizontal ) (scatter nsex Bsex if n<13, mcolor(black) msymbol(S)), ///
xtitle("{bf:Model-Predicted Change in DV}" "(upon changing SES, gender, or race from min to max)", size(medium)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("Dependent Variables(DV): Exposure to Political Intermediaries" "(Country and Year)") ///
legend(order(2 "SES" 4 "Gender (male)") colgap(20) col(1) color(black)) ///
xline(0, lcolor(black)) ymlab(4 "CHL93" 3 "CHL00" 2 "URY94" 1 "URY00" 8 "CHL93" 7 "CHL00" 6 "URY94" 5 "URY00" 12 "CHL93" 11 "CHL00" 10 "URY94" 9 "URY00" , angle(horizontal)) ///
ylab(10.5 "Freq. of pol. disc." 6.5 "Media exposure" 3 "Contacted by party" , angle(horizontal) notick labgap(9)) ///
ytick(4.5 8.5, tlength(37)) ylab(,nogrid) xlab(-.5 -.25 0 .25 .5 .75 1 1.25 1.5 1.75 2.0, grid) yline(4.5 8.5, lcolor(black))
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Figures\legend2.tif", as(tif) replace
*graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_2legend.tif", as(tif) replace width(15000)

drop Bwealth1-sexlower

		*-----------------Second one: Arg, Mex, and DR
eststo clear
*Table A.22
*ARG07
eststo: reg conversemean_std wealth1 educ male if A_A_CountryYear==1 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealtho07=r(b)
mat vwealtho07=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexo07=r(b)
mat vsexo07=r(V)

eststo: reg mediaattention_std wealth1 educ male if A_A_CountryYear==1 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthm07=r(b)
mat vwealthm07=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexm07=r(b)
mat vsexm07=r(V)

mat bwealthp07=.
mat vwealthp07=.
mat bsexp07=.
mat vsexp07=.

*DR10
eststo: reg conversemean_std wealth1 educ male if A_A_CountryYear==32 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealtho10=r(b)
mat vwealtho10=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexo10=r(b)
mat vsexo10=r(V)

eststo: reg mediaattention_std wealth1 educ male if A_A_CountryYear==32 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthm10=r(b)
mat vwealthm10=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexm10=r(b)
mat vsexm10=r(V)

eststo: reg partycontact_std wealth1 educ male if A_A_CountryYear==32 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthp10=r(b)
mat vwealthp10=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexp10=r(b)
mat vsexp10=r(V)

*Mex 2006
eststo: reg conversemean_std wealth1 educ male if A_A_CountryYear==19 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealtho06=r(b)
mat vwealtho06=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexo06=r(b)
mat vsexo06=r(V)

eststo: reg mediaattention_std wealth1 educ male if A_A_CountryYear==19 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthm06=r(b)
mat vwealthm06=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexm06=r(b)
mat vsexm06=r(V)

eststo: reg partycontact_std wealth1 educ male if A_A_CountryYear==19 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthp06=r(b)
mat vwealthp06=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexp06=r(b)
mat vsexp06=r(V)

*Mex 2012
eststo: reg conversemean_std wealth1 educ male if A_A_CountryYear==33 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealtho12=r(b)
mat vwealtho12=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexo12=r(b)
mat vsexo12=r(V)

eststo: reg mediaattention_std wealth1 educ male if A_A_CountryYear==33 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthm12=r(b)
mat vwealthm12=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexm12=r(b)
mat vsexm12=r(V)

eststo: reg partycontact_std wealth1 educ male if A_A_CountryYear==33 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthp12=r(b)
mat vwealthp12=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexp12=r(b)
mat vsexp12=r(V)

*Mex 2018
eststo: reg conversemean_std wealth1 educ male if A_A_CountryYear==56 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealtho18=r(b)
mat vwealtho18=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexo18=r(b)
mat vsexo18=r(V)

eststo: reg mediaattention_std wealth1 educ male if A_A_CountryYear==56 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthm18=r(b)
mat vwealthm18=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexm18=r(b)
mat vsexm18=r(V)

eststo: reg partycontact_std wealth1 educ male if A_A_CountryYear==56 [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthp18=r(b)
mat vwealthp18=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexp18=r(b)
mat vsexp18=r(V)

*for appendix
cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Code\Output\"
esttab est1 est2 est3 est4 est5 using ARGDRstrat.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Table: Correlates of Intermediation Exposure, Argentina and Dominican Republic)
esttab est6 est7 est8 est9 est10 est11 est12 est13 est14 using Mextrat.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Table: Correlates of Intermediation Exposure, Mexico)

mat Bwealth=[bwealthp18\bwealthp12\bwealthp06\bwealthp10\bwealthp07\bwealthm18\bwealthm12\bwealthm06\bwealthm10\bwealthm07\bwealtho18\bwealtho12\bwealtho06\bwealtho10\bwealtho07]
mat Bsex=[bsexp18\bsexp12\bsexp06\bsexp10\bsexp07\bsexm18\bsexm12\bsexm06\bsexm10\bsexm07\bsexo18\bsexo12\bsexo06\bsexo10\bsexo07]

mat Vwealth=[vwealthp18\vwealthp12\vwealthp06\vwealthp10\vwealthp07\vwealthm18\vwealthm12\vwealthm06\vwealthm10\vwealthm07\vwealtho18\vwealtho12\vwealtho06\vwealtho10\vwealtho07]
mat Vsex=[vsexp18\vsexp12\vsexp06\vsexp10\vsexp07\vsexm18\vsexm12\vsexm06\vsexm10\vsexm07\vsexo18\vsexo12\vsexo06\vsexo10\vsexo07]

svmat Bwealth 
svmat Bsex 
svmat Vwealth 
svmat Vsex 
gen nwealth=_n+.125
gen nsex=_n-.125
*gen n=_n
gen wealthupper=Bwealth+1.9645*(Vwealth^.5)
gen wealthlower=Bwealth-1.9645*(Vwealth^.5)
gen sexupper=Bsex+1.9645*(Vsex^.5)
gen sexlower=Bsex-1.9645*(Vsex^.5)

*Figure 8.2B
*no legend
twoway (rcap wealthupper wealthlower nwealth if n<16, color(black) horizontal ) (scatter nwealth Bwealth if n<16, mcolor(black)) ///
(rcap sexupper sexlower nsex if n<16, color(black) horizontal ) (scatter nsex Bsex if n<16, mcolor(black) msymbol(S) msize(medsmall)), ///
xtitle("{bf:Model-Predicted Change in DV}" "(upon changing SES or gender from min to max)", size(medium)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("{bf:Dependent Variables(DV):}" "{bf:Exposure to Political Intermediaries}" "(Country and Year)", size(medium)) ///
legend(off) ///
xline(0, lcolor(black)) ymlab(15 "ARG07" 14 "DOM10" 13 "MEX06" 12 "MEX12" 11 "MEX18" 10 "ARG07" 9 "DOM10" 8 "MEX06" 7 "MEX12" 6 "MEX18" 5 "ARG07" 4 "DOM10" 3 "MEX06" 2 "MEX12" 1 "MEX18" , angle(horizontal) labsize(medsmall)) ///
ylab(13 "Freq. of pol. disc." 8 "Media exposure" 3 "Contact by party" , angle(horizontal) notick labgap(14) labsize(medsmall)) ///
ytick(5.5 10.5, tlength(40)) ylab(,nogrid) xscale(range(-1.5 2.3)) xlab(-1.5 -1 -.5 0 .5 1 1.5 2.0, grid) yline(5.5 10.5, lcolor(black))
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Figures\Chapter 8 Panel B.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_2B.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_2B.pdf", as(pdf) replace 

*with legend
twoway (rcap wealthupper wealthlower nwealth if n<13, color(black) horizontal ) (scatter nwealth Bwealth if n<13, mcolor(black)) ///
(rcap sexupper sexlower nsex if n<13, color(gs8) horizontal ) (scatter nsex Bsex if n<13, mcolor(gs8) msymbol(S) msize(medsmall)), ///
xtitle("Effect size (in standard deviations)" "of change from min to max in SES or gender") ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("DV: Intermediary Form" "(Country and Year)") ///
legend(order(2 "SES" 4 "gender (male)") colgap(20) col(1) color(black)) ///
xline(0, lcolor(black)) ymlab(1 "ARG07" 2 "DOM10" 3 "MEX06" 4 "MEX12" 5 "ARG07" 6 "DOM10" 7 "MEX06" 8 "MEX12" 9 "ARG07" 10 "DOM10" 11 "MEX06" 12 "MEX12" , angle(horizontal)) ///
ylab(10.5 "Freq. of pol. disc." 6.5 "Media exposure" 2.5 "Contacted by a party" , angle(horizontal) notick labgap(9)) ///
ytick(4.5 8.5, tlength(37)) ylab(,nogrid) xlab(-.5 -.25 0 .25 .5 .75 1 1.25 1.5 1.75 2.0, grid) yline(4.5 8.5, lcolor(black))


drop Bwealth1-sexlower

			*Third one awareness-----------------------------
eststo clear
*Table A.23
*ARG07
eststo: reg aware1z wealth1 educ male [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthAR07=r(b)
mat vwealthAR07=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexAR07=r(b)
mat vsexAR07=r(V)

*CHL93
eststo: reg aware3z wealth1 educ male [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthCL93=r(b)
mat vwealthCL93=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexCL93=r(b)
mat vsexCL93=r(V)

*COL14
eststo: reg aware37z wealth1 educ male [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthCOL14=r(b)
mat vwealthCOL14=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexCOL14=r(b)
mat vsexCOL14=r(V)

*DOM10
eststo: reg aware32z wealth1 educ male [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthDR10=r(b)
mat vwealthDR10=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexDR10=r(b)
mat vsexDR10=r(V)

*MEX06
eststo: reg aware19z wealth1 educ male [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthMX06=r(b)
mat vwealthMX06=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexMX06=r(b)
mat vsexMX06=r(V)

*MEX12
eststo: reg aware33z wealth1 educ male [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthMX12=r(b)
mat vwealthMX12=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexMX12=r(b)
mat vsexMX12=r(V)

*MEX18
eststo: reg aware56z wealth1 educ male [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthMX18=r(b)
mat vwealthMX18=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexMX18=r(b)
mat vsexMX18=r(V)

*URY94
eststo: reg aware30z wealth1 educ male [pweight=L_WtWithin]
margin, at((min) wealth1 educ) at((max) wealth1 educ) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bwealthUR94=r(b)
mat vwealthUR94=r(V)
margin, at((min) male) at((max) male) contrast(atcontrast(r)) vce(unconditional) atmeans
mat bsexUR94=r(b)
mat vsexUR94=r(V)

*MEX06 panel: drawn from other Chapter 2 code
mat bwealthMX06p= 2.51533
mat vwealthMX06p=.00954052
mat bsexMX06p=.3515631
mat vsexMX06p=.0011972

*BRA02 panel: drawn from other Chapter 2 code
mat bwealthBRA02=1.572996
mat vwealthBRA02=.00217015
mat bsexBRA02=.4730163
mat vsexBRA02= .00067092

*BRA06 panel: drawn from other Chapter 2 code
mat bwealthBRA06= 1.666687
mat vwealthBRA06= .00610447
mat bsexBRA06=.5226567
mat vsexBRA06= .00173993

*BRA14: drawn from other Chapter 2 code
mat bwealthBR14=1.689012
mat vwealthBR14=.0040221
mat bsexBR14=.3430155 
mat vsexBR14=.0014915


mat Bwealth=[bwealthUR94\bwealthMX18\bwealthMX12\bwealthMX06\bwealthDR10\bwealthCOL14\bwealthCL93\bwealthAR07\bwealthMX06p\bwealthBR14\bwealthBRA06\bwealthBRA02]
mat Bsex=[bsexUR94\bsexMX18\bsexMX12\bsexMX06\bsexDR10\bsexCOL14\bsexCL93\bsexAR07\bsexMX06p\bsexBR14\bsexBRA06\bsexBRA02]

mat Vwealth=[vwealthUR94\vwealthMX18\vwealthMX12\vwealthMX06\vwealthDR10\vwealthCOL14\vwealthCL93\vwealthAR07\vwealthMX06p\vwealthBR14\vwealthBRA06\vwealthBRA02]
mat Vsex=[vsexUR94\vsexMX18\vsexMX12\vsexMX06\vsexDR10\vsexCOL14\vsexCL93\vsexAR07\vsexMX06p\vsexBR14\vsexBRA06\vsexBRA02]

svmat Bwealth 
svmat Bsex 
svmat Vwealth 
svmat Vsex 
gen nwealth=_n+.125
gen nsex=_n-.125
*gen n=_n
gen wealthupper=Bwealth+1.9645*(Vwealth^.5)
gen wealthlower=Bwealth-1.9645*(Vwealth^.5)
gen sexupper=Bsex+1.9645*(Vsex^.5)
gen sexlower=Bsex-1.9645*(Vsex^.5)

*figure 8.3
*no legend
twoway (rcap wealthupper wealthlower nwealth if n<13, color(black) horizontal ) (scatter nwealth Bwealth if n<13, mcolor(black)) ///
(rcap sexupper sexlower nsex if n<13, color(black) horizontal ) (scatter nsex Bsex if n<13, mcolor(black) msymbol(S) msize(medsmall)), ///
xtitle("{bf:Model-Predicted Change in Political Knowledge}" "(upon changing SES or gender from min to max)", size(medium)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("{bf:Dataset, Country, and Year}", size(medium)) ///
legend(order(2 "SES" 4 "Gender (male)") col(2) color(black) position(6)) ///
ytick(8.5, tlength(18) tlwidth(medium)) title("{bf:Dependent Variable: Political Knowledge}", size(medsmall) color(black)) ///
xline(0, lcolor(black)) ymlab(8 "ARG07" 7 "CHL93" 4 "MEX06" 5 "DOM10" 6 "COL14" 2 "MEX18" 3 "MEX12" 9 "MEX06" 12 "BRA02" 11 "BRA06" 10 "BRA14" 1 "URY94", angle(horizontal) labsize(medsmall)) ///
ylab(4.5 "CNEP" 10.5 "Panels", angle(vertical) nogrid notick labgap(14) labsize(medium)) xscale(range(-.1 2.5)) xlab(0 .5 1 1.5 2.0 2.5, grid)  

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 Normative\Figures\Chapter 8 Awareness.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_3.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR8_3.pdf", as(pdf) replace 

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 8 normative\Output\"
esttab est1 est2 est3 est4 est5 est6 est7 est8 using Awarenessstrat.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Table: Correlates of Awareness)
xyz
restore
*-------------------------------------------------------------------------------------------------

collapse (mean) E_FriendTalk_F E_FamTalk_F  E_WorkTalk_F E_NeighTalk_F talkatall [pweight=L_WtWithin], by(A_A_CountryYear)

drop if talkatall==.

drop if A_A==13 
drop if A_A==45
recode talkatall 1=.

egen mean=rowmean(E_FriendTalk_F E_FamTalk_F  E_NeighTalk_F) 
replace mean=. if A_A==102
recode mean .=1

gsort -mean
gen n=_n
recode mean 1=.

label define axis 1 "{bf:BRA02}" 2 KEN12 3 MOZ04 4 "{bf:BRA06}" 5 ESP11 6 "{bf:URY04}" 7 USA04 8 "{bf:MEX12}" 9 ESP15 ///
10 GRC04 11 "{bf:COL14}" 12 USA12 13 "{bf:DOM10}" 14 "{bf:MEX18}" 15 "{bf:URY94}" 16 ZAF09 17 GRC96 18 BGR96 19 "{bf:MEX06}" 20 "{bf:CHL00}" ///
21 HUN98 22 "{bf:ARG07}" 23 "{bf:BRA14}" 24 HUN06 25 ESP93 26 "{bf:CHL93}" 27 ESP04 28 IDN04 29 TWN04 ///
30 ZAF04 31 CHN08 32 IDN09 33 HKG98
label values n axis
gen str FriendTalk_lab="frd"
gen str FamTalk_lab="fam"
gen str NeighTalk_lab="nei" 

/*
twoway (scatter E_FriendTalk_F n, yaxis(1) msym(i) mlab(FriendTalk_lab) mlabpos(0) mlabcolor(black)) ///
(scatter E_FamTalk_F n, yaxis(1) msym(i) mlabpos(0) mlabcolor(black) mlab(FamTalk_lab)) ///
(scatter E_NeighTalk_F n, yaxis(1) msym(i) mlabpos(0) mlabcolor(black) mlab(NeighTalk_lab)) ///
(scatter mean n, yaxis(1) mcolor(black)) ///
(scatter talkatall n, yaxis(2) mcolor(gs8)), ///
xlabel(1(1)33,valuelabel angle(vertical)) ytitle("{bf:Proportion who Discuss Politics}" "(grey points)", color(gs8) axis(2))  ///
ytitle("{bf:Frequency of" "Political Discussion}" "(black points and letters)", axis(1) color(black)) ///
yscale(range(0 1) axis(2)) ytick(0(.25)1, axis(2)) ylab(0 "0%" .25 "25%" .5 "50%" .75 "75%" 1 "100%"), axis(2) labsize(small)) ///
ylabel(, tlcolor(gs8) labcolor(gs8) axis(2)) ///
ylabel(0 "Never" 1 "Rarely" 2 "Sometimes" 3 "Frequently", tlcolor(black) labsize(small) labcolor(black) axis(1)) ///
xtitle("{bf:Country and Year}") graphregion(color(white)) ///
title(" ") ///
plotregion(lstyle(yxline) lcolor(black)) legend(off) ///
note("fam = with family members, frnd = with friends, nei= with neighbors", color(black) span)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Figures\CNEPfreq.tif", as(tif) replace
*/

*Figure 2.2
twoway (scatter E_FriendTalk_F n, yaxis(1) msym(i) mlab(FriendTalk_lab) mlabpos(0) mlabcolor(black) mlabsize(vsmall)) ///
(scatter E_FamTalk_F n, yaxis(1) msym(i) mlabpos(0) mlabcolor(black) mlab(FamTalk_lab) mlabsize(vsmall)) ///
(scatter E_NeighTalk_F n, yaxis(1) msym(i) mlabpos(0) mlabcolor(black) mlab(NeighTalk_lab) mlabsize(vsmall)) ///
(bar mean n, yaxis(1) color(gs8) fcolor(%40) lcolor(black)), ///
xlabel(1(1)33,valuelabel angle(vertical))  ///
ytitle("{bf:Frequency of Political Discussion}", axis(1) color(black)) ///
ylabel(0 "Never" 1 "Rarely" 2 "Sometimes" 3 "Frequently", tlcolor(black) labsize(medsmall) labcolor(black) axis(1)) ///
xtitle("{bf:Country and Year}") graphregion(color(white)) ///
title(" ") ///
plotregion(lstyle(yxline) lcolor(black)) legend(off)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Figures\CNEPfreq1axis.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_2A.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_2A.pdf", as(pdf) replace

gsort -talkatall
gen n2=_n

label define axis2 1 "{bf:BRA02}" 2 ESP15 3 "{bf:BRA06}" 4 USA04 5 "{bf:MEX12}" 6 USA12 7 GRC04 ///
8 GRC96 9 "{bf:COL14}" 10 "{bf:URY94}" 11 HUN98 12 KEN13 13 "{bf:MEX06}"  14 "{bf:URY04}"  15 "{bf:ARG07}" 16 "{bf:MEX18}" 17 MOZ04   ///
  18 ZAF09 19 BGR96 20 "{bf:CHL00}" 21 "{bf:DOM10}" ///
  22 ESP93 23 HUN06 24 "{bf:CHL93}" 25 TWN04 26 IDN04 27 ESP04 28 IDN09 ///
29 CHN08 30 ZAF04 31 HKG98 


label values n2 axis2

twoway (bar talkatall n2 if n2<32, yaxis(1) color(gs2)), ///
xlabel(1(1)31,valuelabel angle(vertical)) ytitle("{bf:Percentage who Discuss Politics}", axis(1))  ///
yscale(range(0 1) axis(1)) ylab(0 "0%" .25 "25%" .5 "50%" .75 "75%" 1 "100%",  axis(1) labsize(medsmall)) ///
ylabel(, tlcolor(black) labcolor(black) axis(1)) ///
xtitle("{bf:Country and Year}") graphregion(color(white)) ///
title(" ") ///
plotregion(lstyle(yxline) lcolor(black)) legend(off)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 2 Description\Figures\CNEPfreq2axis.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_2B.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR2_2B.pdf", as(pdf) replace 
